/**
 * 
 */
package com.renmai.contact;

import com.renmai.R;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author Issac
 *
 */
public class EditContactActivity extends Activity {
	
	private Button editContact;
	private int id;
	private EditText nameText;
	private EditText phoneText;
	private EditText emailText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editcontact);
		String name = "";
		String phone = "";
		String email = "";
		
		Bundle bundle = this.getIntent().getExtras();
		id = bundle.getInt("ids");
		Log.i("ids", id+"****");
		
		
		Cursor phones = getContentResolver().query(
				ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
				null,
				ContactsContract.CommonDataKinds.Phone.CONTACT_ID
						+ " = " + id, null, null);
		
		if (phones.moveToFirst()) {
			do {
				// 遍历所有的电话号码
				phone = phones
						.getString(phones
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				
				int phoneType = phones
						.getInt(phones
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
				if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
					//samContact.phone = phoneNumber;
					break;
				}
			} while (phones.moveToNext());

		}
		phones.close();
		
		Cursor emails = getContentResolver().query(
				ContactsContract.CommonDataKinds.Email.CONTENT_URI,
				null,
				ContactsContract.CommonDataKinds.Email.CONTACT_ID
						+ " = " + id, null, null);
		while (emails.moveToNext()) {
			email = emails
					.getString(emails
							.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
		}
		emails.close();
		
		
		editContact = (Button)findViewById(R.id.edit_contact);
		nameText = (EditText)findViewById(R.id.edit_contact_name);
		phoneText = (EditText)findViewById(R.id.edit_contact_phone);
		emailText = (EditText)findViewById(R.id.edit_contact_email);
		
		nameText.setText(name);
		phoneText.setText(phone);
		emailText.setText(email);
		
		editContact.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				
			}
			
		});
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	/*
	 * query contact
	 */
	public Cursor query(int id){
		Uri person = ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
		Cursor cursor = managedQuery(person, null, null, null, null);
		return cursor;
	}
	
	/*
	 * update contact
	 */
	public void update(int id, String phone, String email){
		ContentValues values = new ContentValues(); 
        values.put(Data.RAW_CONTACT_ID, id);  
        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
        values.put(Phone.NUMBER, phone);  
        values.put(Phone.TYPE, Phone.TYPE_MOBILE);  
		this.getContentResolver().insert(
				android.provider.ContactsContract.Data.CONTENT_URI, values);
        
        values.clear();
        values.put(Data.RAW_CONTACT_ID, id);  
        values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);  
        values.put(Email.DATA, email);  
        values.put(Email.TYPE, Email.TYPE_WORK);  
        this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);  
        
        
	}
	
}
